APM.ConfigPacketfilterController = Ember.Controller.extend({
	init: function(){
		this._super();
		this.initConfData();
	},
	form : {
		policys:"DROP",
		glopolicy:"DROP",
		ip : "",
		ip1 : "",
		port : "",
		port1 : "",
		mac : "",
		policyName:"",
		confType:"4"
	},

	defaultpolicys : Ember.A([{
		"displaytext" : "全部丢弃",
		"value" : "DROP"
	}, {
		"displaytext" : "全部接收",
		"value" : "ACCEPT"
	} ]),

	policyRules: Ember.A(),
	
	checkedAll: Ember.computed('policyRules.@each.checked', function(key, value) {
		var policyRules = this.get('policyRules');

		if (arguments.length > 1) {
			policyRules.forEach(function(item) {
				item.set('checked', value);
			});
		} else {
			return policyRules.filterProperty('checked', true).length == policyRules.length;
		}
	}),

	noneRulesChecked: Ember.computed('policyRules.@each.checked', function(key, value) {
		return this.get('policyRules').filterProperty('checked', true).length == 0;
	}),

	initConfData: function(){
		var ctrl = this;
		ctrl.store.ajax("GET", "configs/systemConfig",{
			configItem:"filter"
		}).then(function(response) {
			var configobj = response.configData;
			if (configobj) {
				if (configobj.filter_default_policy) {
					ctrl.set("form.glopolicy",configobj.filter_default_policy);
				}
				if (configobj.filter_rules) {
					var rules = JSON.parse(configobj.filter_rules);
					var dict = {"DROP": "全部丢弃", "ACCEPT": "全部接收"};
					for (var k = 0; k<rules.length; k++) {
						var policyCode=rules[k].policys;
						if(dict[policyCode]){
							rules[k].policyName=dict[policyCode];
						}
						rules[k].id=k+1;
						rules[k] = Ember.A(rules[k]);
					}
					ctrl.set("policyRules", Ember.A(rules));
				}
			}	
		});
	},
	
	checkFormValid: function(){
		var form = this.get("form");
		
		this.set("formTip", null);
		this.set("saveResult", null);
		
		if(form.ip){
			var reg1=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
			if(!reg1.test(form.ip)){  
			   this.set("saveResult", "开始IP地址范围非法，请重新输入!");
			   return false;
			}  
		}
		if(form.ip1){
			var reg1=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
			if(!reg1.test(form.ip1)){  
			   this.set("saveResult", "结束IP地址范围非法，请重新输入!");
			   return false;
			}  
		}
		if(form.port){
			var reg = new RegExp("^[0-9]*$"); 
			var port=form.port;
			var port1=parseInt(form.port);
			if (!(reg.test(port) && port1<65536)) {
				this.set("saveResult", "开始端口错误，请重新输入!");
				return false;
			} 
		}
		if(form.port1){
			var reg = new RegExp("^[0-9]*$"); 
			var port1=form.port1;
			var port=parseInt(form.port1);
			if (!(reg.test(port1) && port<65536)) { 
				this.set("saveResult", "结束端口错误，请重新输入!");
				return false;
			} 
		}
		if(form.mac){
			var reg2=/[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}/;
			var reg3=/[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}/;
			if(!(reg2.test(form.mac) || reg3.test(form.mac))){  
				   this.set("saveResult", "Mac地址非法，请重新输入!");
				   return false;
			} 
		}
		
		if(form.ip && form.ip1){
			var startIp=form.ip;
			var endIp=form.ip1;
			var startIpint=parseInt(startIp.replace(/\d+\.?/ig,function(a){
								a = parseInt(a); 
								return (a > 15 ? "" : "0") + a.toString(16);
							}),16);
			var endIpint=parseInt(endIp.replace(/\d+\.?/ig,function(b){
				b = parseInt(b); 
				return (b > 15 ? "" : "0") + b.toString(16);
			}),16);
			if(startIpint>endIpint){
				 this.set("saveResult", "开始IP地址不应大于结束IP地址!");
					return false;
			}
			if(startIpint == endIpint){
				 this.set("saveResult", "开始IP地址与结束IP地址相同!");
					return false;
			}
		}
		
		if(form.port && form.port1){
			var startPort=parseInt(form.port);
			var endPort=parseInt(form.port1);
			if(startPort>endPort){
				 this.set("saveResult", "开始端口不应大于结束端口!");
					return false;
			}
			if(startPort == endPort){
				 this.set("saveResult", "开始端口与结束端口相同!");
					return false;
			}
			
		}
		
		if(!(form.ip||form.ip1||form.port||form.port1||form.mac)){
			this.set("saveResult", "不能添加空策略!");
			return false;
		}
		this.set("saveResult", null);
		return true;
	},

	actions : {
		reset : function() {
			this.set("formTip", null);
			this.set("saveResult", null);
		},
		
		addPolicy : function() {
			if (!this.checkFormValid()) {
				return;
			}
			var policyRules = this.get("policyRules");
			var policyObj = {};
			var ctrl = this;
			var form = this.get("form");
			var ipstr="";
			if (form.ip&&form.ip1) {
				 ipstr=form.ip+"-"+form.ip1;
			} else {
				if (form.ip) {
					 ipstr=form.ip;
				}

				if (form.ip1) {
					 ipstr=form.ip1;
				}
			};
			var portstr="";
			if (form.port&&form.port1) {
				 portstr=form.port+"-"+form.port1;
			} else {
				if(form.port){
					 portstr=form.port;
				};
				if(form.port1){
					 portstr=form.port1;
				}
			}
			var addId=1+policyRules.length;
			var dict = {"DROP": "全部丢弃", "ACCEPT": "全部接收"};
			policyObj.ip = ipstr;
			policyObj.id=addId;
			policyObj.port = portstr;
			policyObj.mac = form.mac;
			policyObj.policys=form.policys;
			if(dict[policyObj.policys]){
				policyObj.policyName=dict[policyObj.policys];
			}
			policyRules.pushObject(Ember.A(policyObj));
			ctrl.send("closeDialog");
			this.set("form.ip", null);
			this.set("form.ip1", null);
			this.set("form.port", null);
			this.set("form.port1", null);
			this.set("form.mac", null);
			this.set("policyName",null);
			this.set("form.policys", "DROP");
		},

		delPolicy : function() {
			var policyRules = this.get('policyRules');
			var checked = policyRules.filterProperty('checked', true);
			if (checked.length == 0) {
				alertDialog("请勾选要删除的策略!");
			}
			confirmDialog("确定删除所选过滤策略吗?").then(function(){
				policyRules.removeObjects(checked);
			});
		},

		singleDelPolicy : function(policyRule) {
			var policyRules = this.get('policyRules');	
			confirmDialog("确定删除该过滤策略吗?").then(function(){
				policyRules.removeObject(policyRule);
			});		
		},

		upPriority: function(policyRule){
			var policyRules = this.get("policyRules"),
				index = policyRules.indexOf(policyRule);

			if(index != -1 && index > 0){
				policyRules.removeObject(policyRule);
				policyRules.insertAt(index-1, policyRule);
			}
		},

		downPriority: function(policyRule){
			var policyRules = this.get("policyRules"),
				len = policyRules.length,
				index = policyRules.indexOf(policyRule);

			if(index != -1 && len > 1 && index < len-1 ){
				policyRules.removeObject(policyRule);
				policyRules.insertAt(index+1, policyRule);
			}
		},

		//对话框操作打开方法
		openDialog: function(){
			this.set("formTip", null);
			this.set("saveResult", null);
			var dialog = Broada.getView("packet_filter_add");
			dialog.openDialog();
		},

		//对话框操作打开方法
		updOpenDialog: function(policyRule){
			this.set("formTip", null);
			this.set("saveResult", null);
			var ip=policyRule.ip;
			var port=policyRule.port;
			var ip1=null;
			var ip2=null;
			var port1=null;
			var port2=null;
			if(ip && (ip.indexOf("-") !=-1)){
				var ipIndex=ip.indexOf("-");
				ip1=ip.substr(0,ipIndex);
				ip2=ip.substr(ipIndex+1,ip.length);
			}else{
				ip1=ip;
			}
			if(port && (port.indexOf("-") !=-1)){
				var portIndex=port.indexOf("-");
				port1=port.substr(0,portIndex);
				port2=port.substr(portIndex+1,port.length);
			}else{
				port1=port;
			}
			this.set("id",policyRule.id);
			this.set("form.ip", ip1);
			this.set("form.ip1", ip2);
			this.set("form.port", port1);
			this.set("form.port1", port2);
			this.set("form.mac", policyRule.mac);
			this.set("form.policys", policyRule.policys);
			var dialog = Broada.getView("packet_filter_upd");
			dialog.openDialog();
		},

		updCloseDialog: function(){
			this.set("formTip", null);
			this.set("saveResult", null);
			this.set("form.ip", null);
			this.set("form.ip1", null);
			this.set("form.port", null);
			this.set("form.port1", null);
			this.set("form.mac", null);
			this.set("id",null);
			this.set("form.policyName",null);
			this.set("form.policys","DROP");
			Broada.getView("packet_filter_upd").closeDialog();
		},

		updPolicy : function() {
			if (!this.checkFormValid()) {
				return;
			}
			var policyRules = this.get("policyRules");
			var policyObj = {};
			var ctrl = this;
			var form = ctrl.get("form");
			var updObjId=ctrl.get("id");		

			var dict = {"DROP": "全部丢弃", "ACCEPT": "全部接收"};	
			if(dict[form.policys]){
				this.set("form.policyName",dict[form.policys]);
			}

			ctrl.set("form.ip", Ember.EnumerableUtils.filter([form.ip, form.ip1], function(v) {return !!v;}).join('-'));
			ctrl.set("form.port", Ember.EnumerableUtils.filter([form.port, form.port1], function(v) {return !!v;}).join('-'));

			if(updObjId){
				policyRules.findBy('id', updObjId).setProperties(form);
			}
			ctrl.send("updCloseDialog");
			ctrl.set("form.ip", null);
			ctrl.set("form.ip1", null);
			ctrl.set("form.port", null);
			ctrl.set("form.port1", null);
			ctrl.set("form.mac", null);
			ctrl.set("id",null);
			ctrl.set("form.policyName",null);
			ctrl.set("form.policys","DROP");
		},

		closeDialog: function(){
			this.set("saveResult", null);
			Broada.getView("packet_filter_add").closeDialog();
		},

		queryPolicys:function(){
			var ctrl = this;
			ctrl.store.ajax("GET", "configs/systemConfig",{
			configItem:"filter"
			}).then(function(response) {
				var configobj = response.configData;
				if (configobj) {
					if (configobj.filter_default_policy) {
						ctrl.set("form.glopolicy",configobj.filter_default_policy);
					}
					if (configobj.filter_rules) {
						var rules = JSON.parse(configobj.filter_rules);
						for (var k = 0; k<rules.length; k++) {
							rules[k].id=k+1;
							rules[k] = Ember.A(rules[k]);
						}
						ctrl.set("policyRules", Ember.A(rules));
					}
				}	
			});
		},
		
		save : function() {
			var ctrl = this,
				form = this.get("form"),
				policyRules = this.get('policyRules'),
				rulesArray = [];
			
			ctrl.set("saveResult", null);
			policyRules.forEach(function(item) {
				rulesArray.push(item.getProperties(['ip', 'port','policys']));
			});

			this.store.ajax("POST", "configs/systemConfig", {
				defaultpolicy : form.glopolicy,
				policyRules:JSON.stringify(rulesArray),
				confType:form.confType
			}).then(function(response){
				alertTip("保存成功！");
			});
		}
	}
});